home *** CD-ROM | disk | FTP | other *** search
- /*
- File: FileCtr.cpp
-
- Contains: Implementation of ODFileContainer class.
-
- Owned by: Vincent Lo
-
- Copyright: © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <7> 8/13/96 DM 1376080: TempSuppressFatalBentoError
- spelling
- <6> 5/24/96 jpa 1246074: SOM_CATCH --> SOM_TRY..SOM_ENDTRY
- <4> 5/23/96 DH 1344338: Force quit of document when
- dragging 'Bad' file or Container. Turned
- off fatal Bento errors for Open and Create
- container operations.1333189: When a drag
- of an OpenDoc document fails, document is
- left open. If document fails to open, manually
- close the file.
- <2> 1/15/96 TJ Cleaned Up
- In Progress:
-
- */
-
- #define ODFileContainer_Class_Source
-
- #define VARIABLE_MACROS
-
- #include <FileCtr.xih>
-
- #ifndef _PLFMDEF_
- #include "PlfmDef.h"
- #endif
-
- #ifndef _BENTODEF_
- #include "BentoDef.h"
- #endif
-
- #ifndef SOM_ODStorageSystem_xh
- #include <ODStor.xh>
- #endif
-
- #ifndef _FSHDR_
- #include "FSHdr.h"
- #endif
-
- #ifndef _TARGTHDR_
- #include "TargtHdr.h"
- #endif
-
- #ifndef _INDHDR_
- #include "IndHdr.h"
- #endif
-
- #ifndef __CM_API__
- #include "CMAPI.h"
- #endif
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #ifndef SOM_Module_OpenDoc_Errors_defined
- #include "ErrorDef.xh"
- #endif
-
- #ifndef _ODNEW_
- #include <ODNew.h>
- #endif
-
- #ifndef _PLFMFILE_
- #include <PlfmFile.h>
- #endif
-
- #ifndef _ITEXT_
- #include <IText.h>
- #endif
-
- #ifndef _BARRAY_
- #include <BArray.h>
- #endif
-
- #ifndef __ALIASES__
- #include <Aliases.h>
- #endif
-
- #ifdef DebugRefCount
- #include "Stdio.h"
- #endif
-
- #ifndef _BENTOSUPPRESS_
- #include <BentoSuppress.h>
- #endif
-
- #if ODDebug
- // #define ODDebug_ODFileContainer 1
- #endif
-
- #pragma segment ODFileContainer
-
- //==============================================================================
- // ODFileContainer
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // ODFileContainer: ~ODFileContainer
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODFileContainersomUninit(ODFileContainer *somSelf)
- {
- ODFileContainerData *somThis = ODFileContainerGetData(somSelf);
- ODFileContainerMethodDebug("ODFileContainer","somUninit");
-
- #ifdef DebugRefCount
- printf("~ODFileContainer %x CMContainer %x RefCount %d\n", somSelf, _fCMContainer, _fRefCount);
- fflush(stdout);
- #endif
-
- TRY{
- Environment *ev = somGetGlobalEnvironment ();
-
- if (_fCMContainer != kODNULL)
- somSelf->Close(ev);
-
- if (_fHandlers != kODNULL)
- delete _fHandlers;
-
- if (_fPlatformFile != kODNULL)
- delete _fPlatformFile;
-
- if (_fAlias != kODNULL)
- ODDisposeHandle((ODHandle) _fAlias);
- }CATCH_ALL{
- // Ignore exceptions
- }ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODFileContainer: InitContainer
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODFileContainerInitContainer(ODFileContainer *somSelf, Environment *ev,
- ODStorageSystem* storage, ODContainerID* id)
- {
- ODFileContainerData *somThis = ODFileContainerGetData(somSelf);
- ODFileContainerMethodDebug("ODFileContainer","InitContainer");
-
- SOM_TRY
-
- /* Moved from somInit. SOM itself sets fields to zero
- _fCMContainer = kODNULL;
- _fHandlers = kODNULL;
- _fAlias = kODNULL;
- _fPlatformFile = kODNULL;
- */
-
- OSErr err = NewAlias(kODNULL, (FSSpec*) id->_buffer, &_fAlias);
- if (err != noErr)
- THROW(kODErrCannotCreateContainer);
-
- // Change id to exclude any garbage before FSSpec.name.
- // struct FSSpec {
- // short vRefNum;
- // long parID;
- // Str63 name;
- // };
- id->_length = sizeof(short) + sizeof(long) + ((FSSpec*) id->_buffer)->name[0] + 1;
-
- // Call parent with the updated id
- parent_InitContainer(somSelf, ev, storage, id);
-
- _fPlatformFile = new PlatformFile();
- _fPlatformFile->Specify((ODFileSpec*) id->_buffer);
-
- _fHandlers = new(somSelf->GetHeap(ev)) ODFSBentoHandlers(somSelf->GetCMSession(ev),
- _fPlatformFile);
- _fHandlers->Initialize();
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODFileContainer: GetID
- //------------------------------------------------------------------------------
-
- SOM_Scope ODContainerID SOMLINK ODFileContainerGetID(ODFileContainer *somSelf, Environment *ev)
- {
- ODFileContainerData *somThis = ODFileContainerGetData(somSelf);
- ODFileContainerMethodDebug("ODFileContainer","GetName");
-
- ODByteArray ba;
-
- SOM_TRY
-
- ODBoolean dummyWasChanged;
- ODFileSpec* fsSpec = (ODFileSpec*) ODNewPtrClear(sizeof(ODFileSpec));
-
- OSErr err = ResolveAlias(kODNULL, _fAlias, fsSpec, &dummyWasChanged);
- if (err != noErr) {
- *fsSpec = _fPlatformFile->GetFileSpec();
- }
- ba._buffer = (octet*) fsSpec;
- ba._maximum = sizeof(ODFileSpec);
- ba._length = sizeof(short) + sizeof(long) + ((FSSpec*) ba._buffer)->name[0] + 1;
-
- SOM_CATCH_ALL
- ba._buffer = kODNULL;
- ba._maximum = ba._length = 0;
- SOM_ENDTRY
-
- return ba;
- }
-
- //------------------------------------------------------------------------------
- // ODFileContainer: GetName
- //------------------------------------------------------------------------------
-
- SOM_Scope ODContainerName SOMLINK ODFileContainerGetName(ODFileContainer *somSelf, Environment *ev)
- {
- ODFileContainerData *somThis = ODFileContainerGetData(somSelf);
- ODFileContainerMethodDebug("ODFileContainer","GetName");
-
- ODFileSpec fsSpec;
- ODContainerName* name = kODNULL;
- ODBoolean dummyWasChanged;
-
- SOM_TRY
-
- OSErr err = ResolveAlias(kODNULL, _fAlias, &fsSpec, &dummyWasChanged);
- if (err != noErr) {
- name = _fPlatformFile->GetName();
- }
- else {
- name = CreateIText(0, 0, (StringPtr) &fsSpec.name);
- }
-
- SOM_CATCH_ALL
- SOM_ENDTRY
-
- return *name;
- }
-
- //------------------------------------------------------------------------------
- // ODFileContainer: SetName
- //------------------------------------------------------------------------------
-
- SOM_Scope void SOMLINK ODFileContainerSetName(ODFileContainer *somSelf, Environment *ev,
- ODContainerName* name)
- {
- ODFileContainerData *somThis = ODFileContainerGetData(somSelf);
- ODFileContainerMethodDebug("ODFileContainer","SetName");
-
- SOM_TRY
-
- StringPtr fileName = GetPStringFromIText(name);
- _fPlatformFile->Rename(fileName);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- }
-
- //------------------------------------------------------------------------------
- // ODFileContainer: Create
- //------------------------------------------------------------------------------
-
- SOM_Scope ODContainer* SOMLINK ODFileContainerCreate(ODFileContainer *somSelf, Environment *ev)
- {
- ODFileContainerData *somThis = ODFileContainerGetData(somSelf);
- ODFileContainerMethodDebug("ODFileContainer","Create");
-
- SOM_TRY
-
- if (_fCMContainer == kODNULL) {
-
- somSelf->SetModDate(ev, _fPlatformFile->GetFileModDate());
-
- CMSession cmSession = somSelf->GetCMSession(ev);
-
- CMSetMetaHandler(cmSession,
- (CMGlobalName)CMTargetHandlersTypeName,
- targetContainerMetahandler);
-
- {
- TempSuppressFatalBentoError temp;
-
- _fCMContainer = CMOpenNewContainer(cmSession,
- _fHandlers,
- (CMGlobalName) kODBentoFileTypeName,
- (CMContainerUseMode) (kCMWriting),
- 1, kCMDefaultEndian);
- }
- if (_fCMContainer == kODNULL)
- THROW(kODErrCannotCreateContainer);
- }
-
- return somSelf;
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- return somSelf;
- }
-
- //------------------------------------------------------------------------------
- // ODFileContainer: Open
- //------------------------------------------------------------------------------
-
- SOM_Scope ODContainer* SOMLINK ODFileContainerOpen(ODFileContainer *somSelf, Environment *ev)
- {
- ODFileContainerData *somThis = ODFileContainerGetData(somSelf);
- ODFileContainerMethodDebug("ODFileContainer","Open");
-
- SOM_TRY
-
- if (_fCMContainer == kODNULL) {
-
- somSelf->SetModDate(ev, _fPlatformFile->GetFileModDate());
-
- CMSession cmSession = somSelf->GetCMSession(ev);
-
- CMSetMetaHandler(cmSession,
- (CMGlobalName)CMTargetHandlersTypeName,
- targetContainerMetahandler);
- { // Suppress fatal Bento errors.
- TempSuppressFatalBentoError temp;
-
- TRY
- _fCMContainer = CMOpenContainer(cmSession,
- _fHandlers,
- (CMGlobalName) kODBentoFileTypeName,
- (CMContainerUseMode) kCMReuseFreeSpace);
- CATCH_ALL
- // At this point there was some problem with opening the Container.
- // Unfortunately, Bento will not cleanup after determining there is a
- // problem, which leaves the file open in the file system. We must
- // close it manually then.
- _fPlatformFile->Close();
- return kODNULL; // Propogate the error by not clearing it here and
- // immediately exiting.
- ENDTRY
- }
- if (_fCMContainer == kODNULL)
- THROW(kODErrCannotOpenContainer);
- }
-
- return somSelf;
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- return somSelf;
- }
-
- //------------------------------------------------------------------------------
- // ODFileContainer: Close
- //------------------------------------------------------------------------------
-
- SOM_Scope ODContainer* SOMLINK ODFileContainerClose(ODFileContainer *somSelf, Environment *ev)
- {
- ODFileContainerData *somThis = ODFileContainerGetData(somSelf);
- ODFileContainerMethodDebug("ODFileContainer","Close");
-
- SOM_TRY
-
- if (_fCMContainer != kODNULL) {
- SOM_TRY
- if (somSelf->GetDirtyFlag(ev) != kODFalse) {
- CMObject currObj, nextObj;
- CMProperty embedProp;
- CMType embedType;
- CMValue currValue;
- CMSize valueSize;
-
- embedProp = CMRegisterProperty(_fCMContainer, kODPropVersionList);
- currObj = CMGetNextObjectWithProperty(_fCMContainer, kODNULL, embedProp);
- CMKeepObject(currObj);
- embedProp = CMRegisterProperty(_fCMContainer, kODEmbeddedContainerProperty); // "OpenDoc:EmbeddedContainer"
- embedType = CMRegisterType(_fCMContainer, kODEmbeddedContainerType); // "OpenDoc:EmbeddedContainerType"
- currObj = CMGetNextObjectWithProperty(_fCMContainer, kODNULL, embedProp);
- while (currObj) {
- nextObj = CMGetNextObjectWithProperty(_fCMContainer, currObj, embedProp);
- currValue = CMUseValue(currObj, embedProp, embedType);
- if (currValue) {
- valueSize = CMGetValueSize(currValue);
- CMReleaseValue(currValue);
- if (valueSize) /* no need to keep it unless length is not zero */
- CMKeepObject(currObj);
- }
- currObj = nextObj;
- };
- CMCloseContainer(_fCMContainer);
- #if ODDebug_ODFileContainer
- somPrintf("FileContainer Close: Close\n");
- #endif
- }
- else {
- CMAbortContainer(_fCMContainer);
- #if ODDebug_ODFileContainer
- somPrintf("FileContainer Close: Abort\n");
- #endif
- }
- _fPlatformFile->SetFileModDate(somSelf->GetModDate(ev));
- SOM_CATCH_ALL
- if (ErrorCode() == kODErrBentoErr)
- SetErrorCode(kODErrFatalContainerError);
- SOM_ENDTRY
- _fCMContainer = kODNULL;
- }
-
- return ODFileContainer_parent_ODBentoContainer_Close(somSelf, ev);
-
- SOM_CATCH_ALL
- SOM_ENDTRY
- return somSelf;
- }
-
- //------------------------------------------------------------------------------
- // ODFileContainer: Purge
- //------------------------------------------------------------------------------
-
- SOM_Scope ODSize SOMLINK ODFileContainerPurge(ODFileContainer *somSelf, Environment *ev,
- ODSize size)
- {
- ODFileContainerData *somThis = ODFileContainerGetData(somSelf);
- ODFileContainerMethodDebug("ODFileContainer","Purge");
-
- ODSize freed = 0; ODVolatile(freed);
-
- SOM_TRY
- freed = parent_Purge(somSelf, ev, size);
- SOM_CATCH_ALL
- WARN("Error %ld trying to purge in ODFileContainerPurge",ErrorCode());
- SetErrorCode(kODNoError); // dh - Eat the exception; Purge should not
- // propagate it because clients function
- // fine whether memory was purged or not.
- // Also, don't return 0 if an exception
- // was thrown. Initialized counter should
- // suffice to give correct return value.
- SOM_ENDTRY
-
- return freed;
- }
-
- //------------------------------------------------------------------------------
- // ODFileContainer: GetCMContainer
- //------------------------------------------------------------------------------
-
- SOM_Scope CMContainer SOMLINK ODFileContainerGetCMContainer(ODFileContainer *somSelf, Environment *ev)
- {
- ODFileContainerData *somThis = ODFileContainerGetData(somSelf);
- ODFileContainerMethodDebug("ODFileContainer","GetCMContainer");
-
- return _fCMContainer;
- }
-
- //------------------------------------------------------------------------------
- // ODFileContainer: GetHandlers
- //------------------------------------------------------------------------------
-
- SOM_Scope ODBentoHandlers* SOMLINK ODFileContainerGetHandlers(ODFileContainer *somSelf, Environment *ev)
- {
- ODFileContainerData *somThis = ODFileContainerGetData(somSelf);
- ODFileContainerMethodDebug("ODFileContainer","GetHandlers");
-
- return _fHandlers;
- }
-
-